package com.algorithm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 约翰认为字符串的完美度等于它里面所有字母的完美度之和。每个字母的完美度可以由你来分配，不同字母的完美度不同，分别对应一个1-26之间的整数。
 * 约翰不在乎字母大小写。（也就是说字母F和f）的完美度相同。给定一个字符串，输出它的最大可能的完美度。例如：dad，你可以将26分配给d，25分配给a，这样整个字符串完美度为77。
 * 分析： 由排序不等式，出现次数最多的字母显然应该给26。所以这个题目变成了统计每种字母出现的次数了，然后按照出现次数从大到小，依次分配从高到低的权值。这就是最朴素的贪心思想。
 * Created by houjinxin on 16/5/17.
 */
public class PefectString {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 1 << 16);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out), 1 << 16);
        String input = reader.readLine();
        input= input.toLowerCase();
        writer.write("字符串完美度" + calculatePerfectDegree(input));
        writer.flush();
    }

    public static int calculatePerfectDegree(String perfectString) {
        Map<Character, Integer> map = getCharacterIntegerMap(perfectString);

        List<Character> keys = new ArrayList<>(map.keySet());
        Collections.sort(keys, new ByValueComparator(map));
        int perfectDegree = 26;
        int sumPerfectDegree = 0;
        for (int i = 0; i < keys.size(); i++) {
            sumPerfectDegree += (perfectDegree - i) * map.get(keys.get(i));
            System.out.printf("%s -> %d\n", keys.get(i), map.get(keys.get(i)));
        }
        return sumPerfectDegree;
    }

    public static Map<Character, Integer> getCharacterIntegerMap(String perfectString) {
        char[] chars = perfectString.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        for (char e : chars) {
//            int nums = map.putIfAbsent(e, 1);
//            if (nums >= 1){
//                map.put(e, ++nums);
//            }
            Integer eTimes = map.get(e);
            if (eTimes == null) {
                map.put(e, 1);
            } else {
                map.put(e, ++eTimes);
            }
        }
        return map;
    }

    static class ByValueComparator implements Comparator<Character> {
        Map<Character, Integer> base_map;

        public ByValueComparator(Map<Character, Integer> base_map) {
            this.base_map = base_map;
        }

        @Override
        public int compare(Character o1, Character o2) {
            if (base_map.get(o1) < base_map.get(o2)) {
                return 1;
            } else if (base_map.get(o1) == base_map.get(o2)) {
                return 0;
            } else {
                return -1;
            }
        }
    }
}
